
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class ImagePanel extends JPanel{
	private static final long serialVersionUID = -8745387195708824212L;

	private int x = 0;
	private int y = 0;
	private int offset_x = 0;
	private int offset_y = 0;

	private Image source_image = null;
	private String source_image_path;
	
	public ImagePanel()
	{
		//message = s;
		addMouseMotionListener(new MouseMotionAdapter()
		{
			public void mouseDragged(MouseEvent e)
			{
				x = e.getX();
				y = e.getY();					
				repaint();
			}
		}
		);

		addMouseListener(new MouseAdapter()
		{
			public void mousePressed(MouseEvent e)
			{
				offset_x = e.getX();
				offset_y = e.getY();
			}

			public void mouseReleased(MouseEvent e){
				x = e.getX();
				y = e.getY();					
				repaint();
				
				ImgCutter cutter = ImgCutter.getInstance();
				cutter.setOffsetPoint(offset_x, offset_y);
				cutter.setCropSize(Math.abs(x - offset_x), Math.abs(y - offset_y));
				cutter.batchProcess();
			}
		}
		);
	}

	public void setImagePath(String path){
		source_image = new ImageIcon(path).getImage();
		source_image_path = path;
	}
	
	public int getEndX(){
		return x;
	}
	
	public int getEndY(){
		return y;
	}
	
	public int getOffsetX(){
		return offset_x;
	}
	
	public int getOffsetY(){
		return offset_y;
	}
	protected void paintComponent(Graphics g)
	{
		super.paintComponent(g);

		if(source_image != null)
		{
			g.drawImage(source_image, 0, 0, source_image.getWidth(null), source_image.getHeight(null), this);
		}

		if(offset_y > y){
			int tmp = y;
			y = offset_y;
			offset_y = tmp;
		}
		
		if(offset_x > x){
			int tmp = x;
			x = offset_x;
			offset_x = tmp;
		}
		
		g.drawRect(offset_x, offset_y, Math.abs(x - offset_x), Math.abs(y - offset_y));
	}
}